package com.yiban.demo.Global.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableAsync
@EnableScheduling
public class ThreadPoolConfig {
    //会自动关闭(call `shutdown` automatically,no need to set destroy "shutdown")
    //异步线程池@Async会在这里跑
    @Bean(name = "taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        poolTaskExecutor.setCorePoolSize(5);
        poolTaskExecutor.setMaxPoolSize(10);
        poolTaskExecutor.setThreadNamePrefix("asyncPool-");
        poolTaskExecutor.setQueueCapacity(1000);
        poolTaskExecutor.setKeepAliveSeconds(60*10);
        //不做任何处理
        poolTaskExecutor.setRejectedExecutionHandler((r,e)->{});
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.setAwaitTerminationMillis(60);
        return poolTaskExecutor;
    }
    //定时任务线程池
    @Bean("schedulerPool")
    public ThreadPoolTaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(4);
        scheduler.setRemoveOnCancelPolicy(true);
        scheduler.setThreadNamePrefix("schedulerPool-");
        return scheduler;
    }



}
